package cn.com.infohold.smartcity.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.com.infohold.smartcity.MainApplication;
import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.Dcms;
import cn.com.infohold.smartcity.api.bean.UserInfo;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.StringUtils;
import cn.com.infohold.smartcity.view.QuitDialog;
import common.annotation.ViewInject;
import common.utils.TouchUtils;
import common.view.list.ListView;
import common.view.list.RecyclerView;
import library.utils.DisplayUtil;
import library.utils.Utils;

/**
 * Created by HKYS on 2016-7-22.
 */
public class UserProfileActivity extends ParentActivity {

    @ViewInject(R.id.toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.toolbar_title)
    private TextView mTitleView;

    @ViewInject(R.id.activity_profile_user_list)
    private ListView mListView;

    private List<ItemData> list = new ArrayList<>();
    private InnerAdapter adapter;
    private UserInfo user;

    @Override
    protected Object inflateView() {
        return R.layout.activity_profile_user;
    }

    @Override
    protected void initView(View anchor) {
        setSystemBar();
        user = XmlData.getInstance().getUserInfo();
        if (user == null)
            finish();

        initToolbar();

        mListView.setItemDecoration(DisplayUtil.dp2px(0.5f), getResources().getColor(R.color.ltgray));
        mListView.setAdapter(adapter = new InnerAdapter(list));
        mListView.setOnRecyclerViewListener(new RecyclerView.OnRecyclerViewListener<ItemData>() {
            @Override
            public void onInnerViewClick(View view, ItemData data, int position) {
                super.onInnerViewClick(view, data, position);
            }

            @Override
            public void onItemClick(ItemData data, int position) {
                super.onItemClick(data, position);
            }
        });

        setUserInfo(user);
    }

    protected void initToolbar() {

        mTitleView.setText(getString(R.string.user_profile));
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserInfo(UserInfo userInfo) {
        try {
            if (userInfo != null) {
                this.user = userInfo;
                list.get(0).icon = userInfo;
                list.get(1).desc = userInfo.getName();
                adapter.notifyItemChanged(0, 1);
            }
        } catch (Exception e) {

        }

    }

    private void setUserInfo(UserInfo user) {

        String username = XmlData.getInstance().getUserInfo().getUsername();

        list.clear();
        list.add(new ItemData(getString(R.string.avatar), null, user, true, false));
        list.add(new ItemData(getString(R.string.username), username, null, false, false));
        list.add(new ItemData(getString(R.string.modify_password), null, R.mipmap.icon_enter_arrow, true, false));
        list.add(null);
        list.add(new ItemData(getString(R.string.settings), null, R.mipmap.icon_enter_arrow, true, false));
        list.add(new ItemData(getString(R.string.about), null, R.mipmap.icon_enter_arrow, true, false));
        list.add(null);
        list.add(new ItemData(getString(R.string.quit), null, null, true, false));
        adapter.setOnRecyclerViewListener(new RecyclerView.OnRecyclerViewListener() {
            @Override
            public void onItemClick(Object data, int position) {
                switch (position) {
//                    case 0:
//                        Utils.startActivity(getApplicationContext(), AvatarActivity.class);
//                        overridePendingTransitionFade();
//                        break;
//                    case 2:
//                        Utils.startActivity(getApplicationContext(), PassWordActivity.class);
//                        overridePendingTransitionFade();
//                        break;
//                    case 4:
//                        Utils.startActivity(getApplicationContext(), SettingsActivity.class);
//                        overridePendingTransitionFade();
//                        break;
//                    case 5:
//                        Utils.startActivity(getApplicationContext(), AboutActivity.class);
//                        overridePendingTransitionFade();
//                        break;
                    case 7:
                        QuitDialog.show(getActivity(),
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Utils.startActivity(getApplicationContext(), LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        overridePendingTransitionFade();
                                    }
                                }, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        finishAllActivities();
                                        MainApplication.exit();
                                    }
                                });
                        break;
                }
            }

            @Override
            public void onInnerViewClick(View view, Object data, int position) {
                super.onInnerViewClick(view, data, position);
                if (position == 0) {
//                    Utils.startActivity(getApplicationContext(), AvatarActivity.class);
//                    overridePendingTransitionFade();
                }
            }
        });
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransitionFade();
    }

    private static class InnerAdapter extends ListView.BaseAdapter<ViewHolder> {
        private List<ItemData> list;

        public InnerAdapter(List<ItemData> list) {
            this.list = list;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.item_user_profile, parent, false);
            return new ViewHolder(linearLayout);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            final Context context = holder.anchor.getContext();
            final ItemData data = list.get(position);
            if (data == null) {
                holder.label.setVisibility(View.GONE);
                holder.desc.setVisibility(View.GONE);
                holder.image.setVisibility(View.GONE);
                holder.anchor.setBackgroundColor(context.getResources().getColor(R.color.bg_ltgray));
            } else if (data.icon instanceof UserInfo) {
                holder.anchor.setBackgroundColor(Color.WHITE);
                int minHeight = DisplayUtil.getListItemMinHeight();
                holder.anchor.setMinimumHeight(minHeight * 3 / 2);
                UserInfo userInfo = (UserInfo) data.icon;
                holder.label.setText(StringUtils.fixedKey(data.key));
                holder.image.setVisibility(View.VISIBLE);
                holder.desc.setVisibility(View.GONE);
                int dimen = (int) context.getResources().getDimension(R.dimen.icon_dimen_xxh);
                ViewGroup.LayoutParams layoutParams = holder.image.getLayoutParams();
                layoutParams.width = dimen;
                layoutParams.height = dimen;
                holder.image.setLayoutParams(layoutParams);
                holder.image.setOnTouchListener(TouchUtils.alphaOnTouch());
                holder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getOnRecyclerViewListener().onInnerViewClick(holder.image, data, position);
                    }
                });
                Dcms.loadAvatar(holder.image, userInfo);
            } else {
                holder.anchor.setBackgroundColor(Color.WHITE);

                int minHeight = DisplayUtil.getListItemMinHeight();
                holder.anchor.setMinimumHeight(minHeight);
                holder.label.setText(StringUtils.fixedKey(data.key));
                if (data.desc == null && data.editable && data.icon != null) {
                    holder.image.setVisibility(View.VISIBLE);
                    holder.desc.setVisibility(View.GONE);
                    int dimen = DisplayUtil.dp2px(16);
                    ViewGroup.LayoutParams layoutParams = holder.image.getLayoutParams();
                    layoutParams.width = dimen;
                    layoutParams.height = dimen;
                    holder.image.setLayoutParams(layoutParams);
                    if (data.icon instanceof Integer)
                        holder.image.setImageResource((Integer) data.icon);
                } else if (data.desc != null) {
                    holder.image.setVisibility(View.GONE);
                    holder.desc.setVisibility(View.VISIBLE);
                    holder.desc.setText(data.desc);
                }
                if (data.linkable) {
                    holder.desc.setAutoLinkMask(Linkify.ALL);
                    holder.desc.setMovementMethod(LinkMovementMethod.getInstance());
                } else {
                    holder.desc.setAutoLinkMask(0);
                    holder.desc.setMovementMethod(null);
                }
            }

            if (data != null && data.editable) {
                holder.anchor.setBackgroundResource(R.drawable.bg_touch_white);
                holder.anchor.setOnClickListener(v -> {
                    if (onRecyclerViewListener != null)
                        onRecyclerViewListener.onItemClick(data, position);
                });
            }
        }

        @Override
        public int getItemCount() {
            return list != null ? list.size() : 0;
        }

    }

    private static class BaseViewHolder extends ListView.ViewHolder {

        protected final View anchor;

        public BaseViewHolder(View anchor) {
            super(anchor);
            this.anchor = anchor;
            anchor.setBackgroundResource(R.drawable.bg_touch_white);
        }
    }


    private static class ViewHolder extends BaseViewHolder {

        private final TextView label;
        private final TextView desc;
        private final ImageView image;


        public ViewHolder(LinearLayout anchor) {
            super(anchor);
            this.label = (TextView) anchor.findViewById(R.id.item_user_profile_label);
            this.desc = (TextView) anchor.findViewById(R.id.item_user_profile_desc);
            this.image = (ImageView) anchor.findViewById(R.id.item_user_profile_img);
        }
    }

    private static class ItemData {
        private String key;
        private String desc;
        private Object icon;
        private boolean editable;
        private boolean linkable;


        public ItemData(String key, String desc, Object icon, boolean editable, boolean linkable) {
            this.key = key;
            this.desc = desc;
            this.icon = icon;
            this.editable = editable;
            this.linkable = editable;
        }
    }

}
